package cn.ilikes.apps.collect.services.impls;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.ilikes.apps.collect.pojos.IlikesMyCollects;
import cn.ilikes.apps.collect.pojos.IlikesMyfolder;
import cn.ilikes.apps.collect.pojos.IlikesUsers;
import cn.ilikes.apps.collect.services.IIlikesBaseService;
import cn.ilikes.apps.collect.services.IlikesCollectsService;
import cn.ilikes.apps.collect.services.IlikesCollectsServiceException;
import cn.ilikes.tools.nosql.jpa.query.pages.PageEntitys;

@Component(IlikesCollectsService.SPRING_DEFULT_NAME)
public class LikesCollectsService implements IlikesCollectsService {

	@Resource(name = IIlikesBaseService.SPRING_DEFULT_NAME)
	private IIlikesBaseService llikesBaseService;

	@Override
	public void addTopMyfolder(String regName, IlikesMyfolder likesMyfolder) throws IlikesCollectsServiceException {
		IlikesUsers likesUsers = llikesBaseService.load(regName, IlikesUsers.class);
		likesMyfolder.setIikesUsers(likesUsers);
		llikesBaseService.save(likesMyfolder);

	}

	@Override
	public void addChildMyfolder(String regName, String parentsId, IlikesMyfolder likesMyfolder) throws IlikesCollectsServiceException {
		IlikesUsers likesUsers = llikesBaseService.load(regName, IlikesUsers.class);
		IlikesMyfolder parentslikesMyfolder = llikesBaseService.load(parentsId, IlikesMyfolder.class);
		likesMyfolder.setIikesUsers(likesUsers);
		likesMyfolder.setParents(parentslikesMyfolder);
		llikesBaseService.save(likesMyfolder);

	}

	@Override
	public List<IlikesMyfolder> findTopIlikesMyfolderByUserId(String regName) throws IlikesCollectsServiceException {
		String SQL = "SELECT  myfolder  FROM IlikesMyfolder AS  myfolder  where myfolder.parents = null and  myfolder.iikesUsers =':regName'";
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("regName", regName);
		return llikesBaseService.findByHSQL(SQL, values);
	}

	@Override
	public void addIlikesMyCollects(String likesMyfolderId, IlikesMyCollects likesMyCollects) throws IlikesCollectsServiceException {
		IlikesMyfolder likesMyfolder = llikesBaseService.load(likesMyfolderId, IlikesMyfolder.class);
		likesMyCollects.setLikesMyfolder(likesMyfolder);
		llikesBaseService.save(likesMyCollects);

	}

	@Override
	public List<IlikesMyCollects> findIlikesMyCollectsByfolderId(String likesMyfolderId) throws IlikesCollectsServiceException {
		String SQL = "SELECT  myfolder  FROM IlikesMyCollects  AS  likesMyCollects  where likesMyCollects.likesMyfolder   = ':folderid'";
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("folderid", likesMyfolderId);
		return llikesBaseService.findByHSQL(SQL, values);
	}

	public List<IlikesMyCollects> findIlikesMyContent(String content,int start,int size) throws IlikesCollectsServiceException {
		String SQL = "SELECT  myfolder  FROM IlikesMyCollects  AS  likesMyCollects  " + " where likesMyCollects.content  = ':content'  OR  likesMyCollects.title = ':content'    ";
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("content", content);
		return llikesBaseService.findByHSQL(SQL, values,start,size);
	}

	@Override
	public PageEntitys findIlikesMyCollectsByfolderId(String content,Properties properties, int size) throws IlikesCollectsServiceException {
		
		String SQL = "SELECT  myfolder  FROM IlikesMyCollects  AS  likesMyCollects  " + " where likesMyCollects.content  = ':content'  OR  likesMyCollects.title = ':content'    ";
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("content", content);
		return llikesBaseService.findPageByHSQL(SQL, values,properties, size);
	}

	

}
